package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Follow;
import com.tour.mapper.FollowMapper;
import com.tour.service.FollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Autowired
    FollowMapper followMapper;

    public ResponseWrapper addFollow(Integer mineId,Integer followId){
        if(mineId != null && followId != null){
            if (mineId == followId){
                return ResponseWrapper.markCustom(false,500,"不能关注自己",null);
            }else {
                Follow follow = new Follow();
                follow.setFollowId(followId);
                follow.setMineId(mineId);
                QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
                followQueryWrapper.eq("mine_id",mineId).eq("follow_id",followId);
                int count = followMapper.selectCount(followQueryWrapper);
                if (count>0){
                    return ResponseWrapper.markCustom(false,500,"不能重复关注",null);
                }else {
                    int result = followMapper.insert(follow);
                    if (result == 1){
                        return ResponseWrapper.markCustom(true,200,"关注成功",null);
                    }else {
                        return ResponseWrapper.markError();
                    }
                }
            }


        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper delFollow(Integer mineId,Integer followId){
        if (mineId != null && followId != null){
            QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
            followQueryWrapper.eq("mine_id",mineId).eq("follow_id",followId);
            int result = followMapper.delete(followQueryWrapper);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"取消关注成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }else {
            return ResponseWrapper.markError();
        }
    }
    public ResponseWrapper isFollow(Integer mineId,Integer followId){
        if (mineId != null && followId != null){
            QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
            followQueryWrapper.eq("mine_id",mineId).eq("follow_id",followId);
            int result = followMapper.selectCount(followQueryWrapper);
            Boolean isFollow = false;
            if (result == 1){
                isFollow = true;
                ResponseWrapper wrapper = ResponseWrapper.markSuccess(isFollow);
                wrapper.getData().put("isFollow",isFollow);
                return wrapper;
            }else {
                isFollow = false;
                ResponseWrapper wrapper = ResponseWrapper.markSuccess(isFollow);
                wrapper.getData().put("isFollow",isFollow);
                return wrapper;
            }
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper findMyFollow(Integer mineId,Integer pageNum,Integer pageSize){
        if (mineId != null){
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> followList = followMapper.findMyFollow(page,mineId);
            List<Map> list = followList.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",followList.getTotal());
            wrapper.getData().put("pageNum",followList.getCurrent());
            wrapper.getData().put("pageSize",followList.getSize());
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper findFollowMe(Integer mineId,Integer pageNum,Integer pageSize){
        if (mineId != null){
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> followList = followMapper.findFollowMe(page,mineId);
            List<Map> list = followList.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);

            wrapper.getData().put("total",followList.getTotal());
            wrapper.getData().put("pageNum",followList.getCurrent());
            wrapper.getData().put("pageSize",followList.getSize());
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }

}
